Ext.define('Department', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
		, "Name"   //部门名称
		, "ParentID"   //上级部门
		, "Dlevel"   //级别
		, "Remarks"   //备注

    ],
    idProperty: "Id"
})


Ext.define('SHApp.Sys.Department.Grid', {
    extend: 'SH.Core.BaseTree',
    alias: 'widget.departmenttree',
    requires: ['SHApp.Sys.Department.Form'],
    dataUrl:"api/departmenttree?format=json",
    fields: ["id", "leaf", "text", "level", "parentId", "expanded", "children", "Dlevel", "Remarks"],
    rootVisible: true,
    rootText: "顶级",
    autoSync: true,
    editForm: null,
    pvalue: 0,
    viewConfig: {
        plugins: {
            ptype: 'treeviewdragdrop',
            appendOnly: true
        }
    },
    initComponent: function () {
        var me = this;
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        this.creatTbar();
        Ext.apply(this);
        //var rs = me.getSelectionModel().getSelection();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    listeners: {
        selectionchange: function (sm, selections) {
            //alert(this.btnEditID);
            if (this.down('#' + this.btnEditID) != null) {
                this.down('#' + this.btnEditID).setDisabled(selections.length == 0);
            }
            if (this.down('#' + this.btnDeleteID) != null) {
                this.down('#' + this.btnDeleteID).setDisabled(selections.length == 0);
            }
        }
    },
    creatTbar: function () {
        var me = this;
        me.tbar = ['->', {
            xtype: 'button',
            text: '添加',
            id: me.btnAddID,
            iconCls: 'icon-btn-add',
            handler: function () {
                var win = me.getForm('SHApp.Sys.Department.Form');
                var rs = me.getSelectionModel().getSelection();
                if (rs.length == 0) {
                    Ext.Msg.alert("提示", "请先选择一项再进行添加");
                    return;
                }
                win.loadRecord(new Department({ Id: null, ParentID: rs[0].data.id }));
                win.Show("添加");
            }
        }, ' ', {
            xtype: 'button',
            disabled: true,
            id: me.btnEditID,
            text: '修改',
            iconCls: 'icon-btn-edit',
            handler: function () {
                var rs = me.getSelectionModel().getSelection();
                me.EditData(me.getForm('SHApp.Sys.Department.Form'), rs);
            }
        }, ' ', {
            xtype: 'button',
            disabled: true,
            id: me.btnDeleteID,
            text: '删除',
            iconCls: 'icon-btn-delete',
            handler: function () {
                var rs = me.getSelectionModel().getSelection();
                if (rs.length > 0) {
                    rs = rs[0];
                    if (rs.data.root) {
                        Ext.Msg.alert("提示信息", "根目录不允许删除！");
                        return;
                    }
                    if (rs.isExpandable() || rs.hasChildNodes()) {
                        Ext.Msg.alert("提示信息", "请先删除其子节点，再删除该节点！");
                        return;
                    } else {
                        var content = "确定删除此节点：" + rs.data.text + "？";
                        Ext.Msg.confirm("提示信息", content, function (btn) {
                            if (btn == "yes") {
                                var rs = me.getSelectionModel().getSelection();
                                if (rs.length > 0) {
                                    Ext.Ajax.request({
                                        method: "DELETE",
                                        url: "department?format=json&id=" + rs[0].data.id,
                                        success: function () {
                                            me.store.load();
                                            me.view.select(0);
                                            me.view.focus(false);
                                        },
                                        failure: function () {
                                            Ext.Msg.alert("提示信息", "删除失败！");
                                        }
                                    });
                                    me.view.select(0);
                                    me.view.focus(false);
                                }
                            }
                        }, me)
                    }
                }
            }
        }
        ]
    },
    getForm: function (form) {
        var me = this;
        if (me.deitForm == null || me.editForm.isDestroy) {
            var frm = Ext.create(form);
            frm.on("OnSubmit", function (action) {
                me.store.load();
            });
            me.editForm = frm;
        } else {
            if (me.editForm.$className != form) {
                var frm = Ext.create(form);
                frm.on("OnSubmit", function (action) {
                    me.store, load();
                });
                me.editForm = frm;
            }
        }
        return me.editForm;
    },
    EditData: function (win, rs) {
        win.loadRecord(new Department({ Id: rs[0].data.id, ParentID: rs[0].data.parentId, Name: rs[0].data.text, Dlevel: rs[0].data.Dlevel, Remarks: rs[0].data.Remarks}));
        win.Show("编辑：" + rs[0].data.text);
    }
})

